<?php
/**
 * QuestionFeedback.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-08-07 11:22:52
 * @modified    2023-08-07 11:22:52
 *
 */

namespace App\Models;

use App\Models\QuestionFeedback\History;
use App\Models\QuestionFeedback\Image;
use App\Repositories\QuantityUserRepo;
use App\Repositories\QuestionFeedbackRepo;
use Carbon\Carbon;

class QuestionFeedback extends BaseModel
{
    protected $table = 'question_feedback';
    protected $primaryKey = 'id';

    public const CREATED_AT = 'create_time';
    public const UPDATED_AT = 'update_time';

    protected $fillable = [
        'qf_number',
        'user_id',
        'user_name',
        'wo_id',
        'wo_number',
        'project_id',
        'project_number',
        'order_number',
        'checked',
        'is_quality',
        'is_return',
        'product_id',
        'product_name',
        'product_number',
        'product_date',
        'quantity',
        'production_base',
        'question_level',
        'exception_level',
        'question_type',
        'question_type_detail',
        'bom_replace',
        'customer_name',
        'manager_id',
        'manager_name',
        'quantity_user_id',
        'quantity_user_name',
        'description',
        'reason',
        'measure_temp',
        'suggestion_manager',
        'suggestion_manager_time',
        'dept_id',
        'dept_name',
        'charge_user_id',
        'charge_user_name',
        'improve',
        'suggestion',
        'measure_reason',
        'measure_temp_scene',
        'quantity_update_time',
        'status',
        'resolved',
        'resolve_time',
        'resolve_content',
        'detectivity'
    ];

    protected $appends = [
        'btn_quantity_audit', // 品质工程师审核
        'btn_engineer_process', // 现场工程师处理
        'btn_close', // 关闭
        'btn_engineer_to', // 反馈至品质部
        'timeout'
    ];

    /**
     * 是否超时
     * 创建24小时候仍未处理
     * @return bool
     */
    public function getTimeoutAttribute()
    {
        $createTime = $this->create_time;
        if (Carbon::now()->diffInHours($createTime) > 24 && $this->status == QuestionFeedbackRepo::STATUS_CREATE) {
            return true;
        }
        return false;
    }

    /**
     * 品质工程师审核按钮
     * 当前用户非本条数据品质工程师: false
     * 当前用户是本条数据品质工程师: 区域经理已审核 ? true : false
     *
     * @return bool
     */
    public function getBtnQuantityAuditAttribute()
    {
        $userId = registry('user_id');
        $currentQuantityUserId = $this->quantity_user_id;
        // 登录用户非当前数据品质工程师
        if ($userId != $currentQuantityUserId) {
            return false;
        }

        // 当前状态非工程师已审核
        if ($this->status != QuestionFeedbackRepo::STATUS_AUDIT) {
            return false;
        }
        return true;
    }

    /**
     * 现场工程师处理
     * 当前用户非创建人: false
     * 当前用户是创建人
     *  品质工程师已审核 ? true : false
     * @return bool
     */
    public function getBtnEngineerProcessAttribute()
    {
        $userId = registry('user_id');
        $createUserId = $this->user_id;
        // 当前用户非创建人
        if ($userId != $createUserId) {
            return false;
        }
        // 当前状态为非品质工程师已审核
        if ($this->status != QuestionFeedbackRepo::STATUS_AUDIT_QUANTITY) {
            return false;
        }
        return true;
    }

    /**
     * 关闭按钮
     * 登录用户非当前数据区域经理: false
     * 当前状态非 已创建 和 反馈人已处理: false
     * @return bool
     */
    public function getBtnCloseAttribute()
    {
        $userId = registry('user_id');
        $managerId = $this->manager_id;
        if ($userId != $managerId) {
            return false;
        }
        if ($this->status != QuestionFeedbackRepo::STATUS_CREATE && $this->status != QuestionFeedbackRepo::STATUS_PROCESSED) {
            return false;
        }
        return true;
    }

    /**
     * 反馈至品质部
     * 登录用户非当前数据区域经理: false
     * 当前状态非已创建: false
     * @return bool
     */
    public function getBtnEngineerToAttribute()
    {
        $userId = registry('user_id');
        $managerId = $this->manager_id;
        if ($userId != $managerId) {
            return false;
        }

        if ($this->status != QuestionFeedbackRepo::STATUS_CREATE) {
            return false;
        }
        return true;
    }

    public function scopeByNumber($query, $number)
    {
        return $query->where('qf_number', $number);
    }

    public function histories()
    {
        return $this->hasMany(History::class, 'qf_id');
    }

    public function images()
    {
        return $this->hasMany(Image::class, 'qf_id');
    }
}